// pages/cart/cart.js
var app = getApp();
const { extend, Stepper } = require('../../zanui/dist/index');
var util = require('../../api/api.js')

Page(extend({}, Stepper,
    {
        /**
         * 页面的初始数据
         */
        data: {
            cartGoods: [
                {
                    productId: '150642571432842',
                    checked: true,
                    productImg: 'https://resource.smartisan.com/resource/71432ad30288fb860a4389881069b874.png',
                    productName: '畅呼吸智能空气净化器',
                    spec: '标准版 白色',
                    productNum: 1,
                    salePrice: '1299.00',
                    stepper: {
                        stepper: 1,
                        min: 1,
                        max: 50
                    },
                },
                {
                    productId: '150642571432843',
                    checked: true,
                    productImg: 'https://resource.smartisan.com/resource/c71ce2297b362f415f1e74d56d867aed.png',
                    productName: '坚果 Pro 2',
                    spec: '纯白色（限量版） 6G + 128G',
                    productNum: 1,
                    salePrice: '1799.00',
                    stepper: {
                        stepper: 1,
                        min: 1,
                        max: 50
                    },
                }
            ],
            checkedGoodsCount: 2,
            checkedGoodsAmount: '',
            isEditCart: false,
            isLogin: false,
            checkedAllStatus: true
        },
        handleZanStepperChange(e) {
            var componentId = e.componentId;
            var stepper = e.stepper;
            this.setData({
                [`cartGoods[${componentId}].stepper.stepper`]: stepper,
                [`cartGoods[${componentId}].productNum`]: stepper,
            });
            this.setData({
                checkedGoodsCount: this.getCheckedGoodsCount(),
                checkedGoodsAmount: this.getCheckedGoodsAmount()
            })
        },
        /**
         * 生命周期函数--监听页面加载
         */
        onLoad: function (options) {
            wx.setNavigationBarTitle({
                title: '购物车'
            });

        },

        /**
         * 生命周期函数--监听页面初次渲染完成
         */
        onReady: function () {

        },

        /**
         * 生命周期函数--监听页面显示
         */
        onShow: function () {
            if (app.globalData.isLogin) {
                this.getCartAll();
                this.setData({
                    isLogin: true
                })
            }

            this.setData({
                checkedGoodsCount: this.getCheckedGoodsCount(),
                checkedGoodsAmount: this.getCheckedGoodsAmount()
            });
        },
        getCartAll() {
            util.cartList().then((res) => {
                if (res.code == 200) {
                    console.log(res, '购物购物车数据')
                }
            }).catch((err) => {
                console.log(err, '验证失败');
            })
        },
        /**
         * 生命周期函数--监听页面隐藏
         */
        onHide: function () {

        },

        /**
         * 生命周期函数--监听页面卸载
         */
        onUnload: function () {

        },
        /**
         * 用户点击右上角分享
         */
        onShareAppMessage: function () {

        },
        goLogin: function () {
            wx.navigateTo({
                url: '/pages/auth/login/login',
            })
        },
        checkedItem: function (e) {
            let index = e.target.dataset.index
            let id = e.target.dataset.id
            this.setData({
                [`cartGoods[${index}].checked`]: !this.data.cartGoods[index].checked,
                checkedGoodsCount: this.getCheckedGoodsCount(),
                checkedGoodsAmount: this.getCheckedGoodsAmount()
            });
            this.setData({
                checkedGoodsCount: this.getCheckedGoodsCount(),
                checkedGoodsAmount: this.getCheckedGoodsAmount()
            });
        },
        getCheckedGoodsAmount: function () {
            let totalPrice = 0
            this.data.cartGoods.forEach(function (v) {
                if (v.checked === true) {
                    totalPrice += v.salePrice * v.productNum
                }
            })
            return totalPrice.toFixed(2)
        },
        getCheckedGoodsCount: function () {
            let checkedGoodsCount = 0
            this.data.cartGoods.forEach(function (v) {
                if (v.checked === true) {
                    checkedGoodsCount += v.productNum
                }
            })
            return checkedGoodsCount
        },
        editCart: function () {
            this.setData({
                isEditCart: !this.data.isEditCart
            });
        },
        checkedAll: function () {
            if (this.data.checkedAllStatus) {
                this.setData({
                    checkedAllStatus: !this.data.checkedAllStatus,
                    checkedGoodsCount: '',
                    checkedGoodsAmount: '0.00'
                });
                for (var i = 0; i < this.data.cartGoods.length; i++) {
                    this.setData({
                        [`cartGoods[${i}].checked`]: false,
                    })
                }
            } else {
                for (var i = 0; i < this.data.cartGoods.length; i++) {
                    this.setData({
                        [`cartGoods[${i}].checked`]: true,
                    })
                }
                this.setData({
                    checkedAllStatus: !this.data.checkedAllStatus,
                    checkedGoodsCount: this.getCheckedGoodsCount(),
                    checkedGoodsAmount: this.getCheckedGoodsAmount()
                });
            }
        },
        deleteCart: function () {
            let that = this;
            let productIds = that.data.cartGoods.filter(i => {
                return i.checked
            }).map(item => {
                return item.productId
            }).join(',')
            wx.showModal({
                title: '提示',
                content: '确认要删除所选购物车商品？',
                confirmColor: '#b4282d',
                success: function (res) {
                    if (res.confirm) {
                        util.cartDel({
                            productId: productIds
                        }).then((res) => {
                            if (res.code == 200) {
                                that.getCartAll();
                            } else {
                                console.log('删除失败');
                            }
                        }).catch((err) => {
                            console.log(err, '验证失败');
                        })
                        that.setData({
                            cartGoods: []
                        })
                    } else if (res.cancel) {

                    }
                }
            })
        },
        checkoutOrder: function () {
            wx.navigateTo({
                url: '/pages/shopping/checkout/checkout',
            })
        }
    })
)